package com.icity.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.icity.entity.Menu;
import com.icity.entity.Role;
import com.icity.entity.RoleUser;
import com.icity.entity.User;
import com.icity.entity.system.RoleMenu;
@Mapper
public interface SystemMapper {

	//通过角色查询可见菜单
    @Select("select * from tbl_statistics_menu where unique_code in "
    		+ "(select menu_code from tbl_statistics_role_menu where role_code = #{role_code}) "
    		+ "and status = 1 order by torder")
	List<Menu> getMenuListByRolesCode(Map<Object, Object> map);

	//通过用户id查询用户所属角色的role_code
    @Select("select * from tbl_statistics_menu where unique_code in "
    		+ "(select menu_code from tbl_statistics_role_menu where role_code = #{role_code})")
    String getRoleCodeByUserCode(Map<String,String> map);

    //查询菜单数量
    @Select("select name from tbl_statistics_menu where unique_code = #{parent_code}")
    String getMenuParentNameBycode(@Param("parent_code") String parent_code);

    //查询菜单数量
	@Select("<script>"
			+"select count(id) from tbl_statistics_menu where 1 = 1"
			+ "<if test='name != null'>"
			+ " and name like CONCAT('%','${name}','%' ) "
			+ "</if>"
			+ "<if test='status != null'>"
			+ " and status = #{status} "
			+ "</if>"
			+ " </script>")
    int getMenuCount(Map<Object,Object> map);

    //查询菜单
	@Select("<script>"
			+"select * from tbl_statistics_menu where 1 = 1 "
			+ "<if test='name != null'>"
			+ " and name like CONCAT('%','${name}','%') "
			+ "</if>"
			+ "<if test='status != null'>"
			+ " and status = #{status} "
			+ "</if>"
			+ " ORDER BY update_time desc "
			+ "limit #{page},#{limit}"
			+ " </script>")
    List<Menu> getMenuList(Map<Object,Object> map);

    @Delete("delete from tbl_statistics_menu where id = #{id}")
    int deleteMenuById(@Param("id") int id);

    @Delete("delete from tbl_statistics_user where id = #{id}")
    int deleteUserById(@Param("id") int id);

    @Select("select * from tbl_statistics_menu where parent_code = '0' ")
    List<Menu> getParentMenu();

    @Select("select * from tbl_statistics_role where parent_code <> '0' ")
    List<Role> getParentUser();

	@Insert("insert into tbl_statistics_menu (unique_code,name,icon_code,url,parent_code,status,"
			+ "torder,create_time,update_time,poster,describes) "
			+ "values (#{unique_code},#{name},#{icon_code},#{url},#{parent_code},#{status},"
			+ "#{torder},#{create_time},#{update_time},#{poster},#{describes})")
	int insertMenu(Menu menu);//新增菜单

	@Insert("insert into tbl_statistics_role_menu (unique_code,role_code,menu_code,create_time,update_time) "
			+ "values (#{unique_code},#{role_code},#{menu_code},"
			+ "#{create_time},#{update_time})")
	int insertRoleMenu(RoleMenu roleMenu);//新增菜单权限

	@Insert("insert into tbl_statistics_user (unique_code,parent_code,name,poster,phone,pwd,password,is_use,status,create_time,update_time,username)"
			+ "values (#{unique_code},#{parent_code},#{name},#{poster},#{phone},#{pwd},#{password},#{is_use},#{status},#{create_time},#{update_time},#{username})")
	int insertUser(User user);//新增用户

	@Insert("insert into tbl_statistics_role_user (unique_code,role_code,partner_code,create_time,update_time) "
			+ "values (#{unique_code},#{role_code},#{partner_code},"
			+ "#{create_time},#{update_time})")
	int insertRoleUser(RoleUser roleUser);//新增用户角色

	@Update("update tbl_statistics_menu set name=#{name},icon_code=#{icon_code},url=#{url},parent_code=#{parent_code},"
			+ "status=#{status},torder=#{torder},update_time=#{update_time},poster=#{poster},describes=#{describes} "
			+ "	where id=#{id}")
	int updateMenu(Menu menu);//修改菜单

	@Update("update tbl_statistics_user set name=#{name},phone=#{phone},poster=#{poster},"
			+ "pwd=#{pwd},password=#{password},is_use=#{is_use},update_time=#{update_time},"
			+ "username=#{username} "
			+ "where id = #{id}")
	int updateUser(User user);//修改用户

	@Update("update tbl_statistics_user set pwd=#{pwd}"
			+ "where phone = #{phone}")
	int updatePwd(@Param("phone") String phone,@Param("pwd")String pwd);//修改密码

	@Update("update tbl_statistics_role_user set role_code=#{role_code}"
			+ "where partner_code = #{partner_code}")
	int updateRoleUser(RoleUser user);//修改用户角色

	@Select("select count(id) as count from tbl_statistics_menu where id = #{id}")
	int getMenuCountByName(@Param("id") int id);

	@Select("select count(id) as count from tbl_statistics_user where id = #{id}")
	int getUserCountByName(@Param("id") int id);

    //查询用户信息，登录
	@Select("select * from tbl_statistics_user where phone = #{name} and pwd = #{password} and is_use = 1")
    List<User> getUserInfo(Map<Object,Object> map);

    //查询用户
	@Select("<script>"
			+"select count(id) as count from tbl_statistics_user where 1 = 1 "
			+ "<if test='name != null'>"
			+ " and name like CONCAT('%','${name}','%') "
			+ "</if>"
			+ "<if test='phone != null'>"
			+ " and phone like CONCAT('%','${phone}','%') "
			+ "</if>"
			+ "<if test='is_use != null'>"
			+ " and is_use = #{is_use} "
			+ "</if>"
			+ " </script>")
    int getUserCount(Map<Object,Object> map);

    //查询用户
	@Select("<script>"
			+"select * from tbl_statistics_user where 1 = 1 "
			+ "<if test='name != null'>"
			+ " and name like CONCAT('%','${name}','%') "
			+ "</if>"
			+ "<if test='phone != null'>"
			+ " and phone like CONCAT('%','${phone}','%') "
			+ "</if>"
			+ "<if test='is_use != null'>"
			+ " and is_use = #{is_use} "
			+ "</if>"
			+ " ORDER BY update_time desc "
			+ "limit #{page},#{limit}"
			+ " </script>")
    List<User> getUserList(Map<Object,Object> map);


    //根据用户id查询用户所属角色名称
	@Select("select name from tbl_statistics_role where "
			+ "unique_code = (select role_code from tbl_statistics_role_user where"
			+ " partner_code = #{partner_code})")
    String getRoleNameByUserId(@Param("partner_code") String partner_code);
}
